package cn.tedu.smartos.family_user.controller;

import cn.tedu.smartos.base.response.JsonResult;
import cn.tedu.smartos.family_user.pojo.entity.LocationRecord;
import cn.tedu.smartos.family_user.pojo.vo.LocationRecordVO;
import cn.tedu.smartos.family_user.service.LocationService;
import cn.tedu.smartos.family_user.service.PositionAlertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "老人位置管理模块")
@Slf4j
@RestController
@RequestMapping("/api/v1/family_user/location")
public class LocationController {
    @Autowired
    private LocationService locationService;
    @Autowired
    private PositionAlertService positionAlertService;
    @ApiOperation("获取老人最新位置")
    @GetMapping("/latest/{elderlyUserId}")
    public JsonResult getLatestLocation(
            @ApiParam(value = "老人用户ID", required = true)
            @PathVariable Long elderlyUserId
    ) {
        log.info("查询老人最新位置, 老人ID: {}", elderlyUserId);
        LocationRecordVO location = locationService.getLatestLocation(elderlyUserId);
        return JsonResult.ok(location);
    }

    @ApiOperation("获取老人位置历史")
    @GetMapping("/history/{elderlyUserId}")
    public JsonResult getLocationHistory(
            @ApiParam(value = "老人用户ID", required = true)
            @PathVariable Long elderlyUserId,

            @ApiParam(value = "返回记录数量，默认10条")
            @RequestParam(required = false, defaultValue = "10") Integer limit
    ) {
        log.info("查询老人位置历史, 老人ID: {}, 数量: {}", elderlyUserId, limit);
        return JsonResult.ok(locationService.getLocationHistory(elderlyUserId, limit));
    }

    @ApiOperation("添加位置记录（设备端调用）")
    @PostMapping("/add")
    public JsonResult addLocationRecord(@RequestBody LocationRecord record) {
        log.info("添加位置记录, 老人ID: {}", record.getElderlyUserId());
        locationService.addLocationRecord(record);
        return JsonResult.ok("位置记录添加成功");
    }
}